package com.exmaple.lemlin.ui.fragment;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.exmaple.lemlin.R;
import com.exmaple.lemlin.callback.OnItemClickFriend;
import com.exmaple.lemlin.entity.FriendGood;
import com.exmaple.lemlin.ui.abstracts.AbstractBaseFragment;
import com.exmaple.lemlin.ui.activity.FriendActivity;
import com.exmaple.lemlin.ui.adapter.UserAdapter;
import com.exmaple.lemlin.utils.JsonUtils;
import com.exmaple.lemlin.utils.LogUtils;
import com.exmaple.lemlin.utils.ToastUtil;

import org.jivesoftware.smack.packet.Presence;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FriendSearchFragment extends AbstractBaseFragment implements FriendActivity.OnFriendSearchListener, UserAdapter.OnItemClickListener {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private List<FriendGood> friends = new ArrayList<>();
    private UserAdapter userAdapter;

    private OnItemClickFriend mOnItemClickFriend;

    public static FriendSearchFragment newInstance() {
        return new FriendSearchFragment();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mOnItemClickFriend = (OnItemClickFriend) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_friend_search;
    }

    @Override
    protected void initView() {

        userAdapter = new UserAdapter(getActivity(), friends);
        userAdapter.setOnItemClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(userAdapter);
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initLazyLoad() {

    }

    @Override
    public void onItemClick(View view, int position) {

        if (mOnItemClickFriend != null) {
            mOnItemClickFriend.onItemClickSearch(view, position, friends.get(position));
        }
    }

    @Override
    public void onFriendGoods(List<FriendGood> friendGoods) {
        userAdapter.addData(friendGoods);
        LogUtils.e(JsonUtils.objectToString(friendGoods));
    }

    @OnClick({R.id.lly_scan})
    public void onViewClicked(View view) {
        ToastUtil.showShort(getActivity(), "功能未开放");
    }
}
